<?php
namespace app\index\controller;
use app\admin\model\EquipmentModel;
use app\admin\model\OrganizationModel;
use app\admin\model\EquipmentTestModel;
use app\admin\model\ConfigModel;
use app\admin\model\ProjectModel;
use app\admin\model\UserModel;
use app\index\model\IndexProjectModel;
use app\admin\model\ProcessesModel;
use think\Db;

class Upload extends Base
{
    //图片上传
    public function upload(){
        $file = request()->file('file');
        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads/images');
        if($info){
             echo $info->getSaveName();
         }else{
             echo $file->getError();
         }
     }
 
     //会员头像上传
     public function uploadface(){
        $file = request()->file('file');
        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads/face');
        if($info){
             return json(['code'=>1,'msg'=>'上传成功','data'=>$info->getSaveName()]);
         }else{
             return json(['code'=>0,'msg'=>$file->getError()]);
         }
     }
 
    /**
     * 上传图片
     */
    public function uploadBase64Image(){
        $base64Data = input('base64Data');
        if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64Data, $result)){

            //图片后缀
            $type = $result[2];

            //保存位置--图片名
            $image_name=date('His').str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT).".".$type;
            $image_url = '/uploads/images/'.date('Ymd').'/'.$image_name;           
            if(!is_dir(dirname('.'.$image_url))){
                mkdir(dirname('.'.$image_url));
                chmod(dirname('.'.$image_url), 0777);
                $oldumask=umask(0); 
                umask($oldumask);

            }
            //解码
            $decode=base64_decode(str_replace($result[1], '', $base64Data));
            if (file_put_contents('.'.$image_url, $decode)){
                $data['code']=1;
                $data['imageName']=date('Ymd').'/'.$image_name;
                $data['url']=$image_url;
                $data['msg']='保存成功！';
            } else {
                $data['code']=0;
                $data['imgageName']='';
                $data['url']='';
                $data['msg']='图片保存失败！';
            }
        } else{
            $data['code']=0;
            $data['imgageName']='';
            $data['url']='';
            $data['msg']='base64图片格式有误！';
        }    
        return json($data);
    }

    /**
     * 上传图片
     */
    public function uploadBase64Face(){
        $base64Data = input('base64Data');
        if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64Data, $result)){

            //图片后缀
            $type = $result[2];

            //保存位置--图片名
            $image_name=date('His').str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT).".".$type;
            $image_url = '/uploads/face/'.date('Ymd').'/'.$image_name;           
            if(!is_dir(dirname('.'.$image_url))){
                mkdir(dirname('.'.$image_url));
                chmod(dirname('.'.$image_url), 0777);
                $oldumask=umask(0); 
                umask($oldumask);

            }
            //解码
            $decode=base64_decode(str_replace($result[1], '', $base64Data));
            if (file_put_contents('.'.$image_url, $decode)){
                $data['code']=1;
                $data['imageName'] = date('Ymd').'/'.$image_name;
                $data['url']=$image_url;
                $data['msg']='保存成功！';
            } else {
                $data['code']=0;
                $data['imgageName']='';
                $data['url']='';
                $data['msg']='图片保存失败！';
            }
        } else{
            $data['code']=0;
            $data['imgageName']='';
            $data['url']='';
            $data['msg']='base64图片格式有误！';
        }    
        return json($data);
    }
}
